package storage

import (
	"fmt"

	"gitee.com/huangxinbo/xcas/internal/config"
)

// StorageFactory creates storage instances based on configuration
type StorageFactory struct {
	config *config.UploadConfig
}

// NewStorageFactory creates a new StorageFactory
func NewStorageFactory(config *config.UploadConfig) *StorageFactory {
	return &StorageFactory{
		config: config,
	}
}

// CreateStorage creates a storage instance based on the configured storage type
func (f *StorageFactory) CreateStorage() (Storage, error) {
	storageType := StorageType(f.config.StorageType)

	switch storageType {
	case StorageTypeLocal:
		return f.createLocalStorage()
	case StorageTypeAliyun:
		return f.createAliyunOSS()
	case StorageTypeHuawei:
		return f.createHuaweiOBS()
	case StorageTypeTencent:
		return f.createTencentCOS()
	case StorageTypeBaidu:
		return f.createBaiduBOS()
	case StorageTypeAWS:
		return f.createAWSS3()
	case StorageTypeAzure:
		return f.createAzureBlob()
	case StorageTypeQiniu:
		return f.createQiniuKodo()
	case StorageTypeGoogle:
		return f.createGoogleStorage()
	case StorageTypeMinIO:
		return f.createMinIO()
	default:
		return nil, fmt.Errorf("unsupported storage type: %s", f.config.StorageType)
	}
}

// createLocalStorage creates a local storage instance
func (f *StorageFactory) createLocalStorage() (Storage, error) {
	return NewLocalStorage(f.config.LocalPath), nil
}

// createAliyunOSS creates an Aliyun OSS storage instance
func (f *StorageFactory) createAliyunOSS() (Storage, error) {
	if f.config.AliyunEndpoint == "" || f.config.AliyunAccessKeyID == "" || f.config.AliyunAccessKeySecret == "" || f.config.AliyunBucketName == "" {
		return nil, fmt.Errorf("Aliyun OSS configuration is incomplete")
	}
	return NewAliyunOSS(f.config), nil
}

// createHuaweiOBS creates a Huawei OBS storage instance
func (f *StorageFactory) createHuaweiOBS() (Storage, error) {
	if f.config.HuaweiEndpoint == "" || f.config.HuaweiAccessKeyID == "" || f.config.HuaweiAccessKeySecret == "" || f.config.HuaweiBucketName == "" {
		return nil, fmt.Errorf("Huawei OBS configuration is incomplete")
	}
	return NewHuaweiOBS(f.config), nil
}

// createTencentCOS creates a Tencent COS storage instance
func (f *StorageFactory) createTencentCOS() (Storage, error) {
	if f.config.TencentEndpoint == "" || f.config.TencentSecretID == "" || f.config.TencentSecretKey == "" || f.config.TencentBucketName == "" {
		return nil, fmt.Errorf("Tencent COS configuration is incomplete")
	}
	return NewTencentCOS(f.config), nil
}

// createBaiduBOS creates a Baidu BOS storage instance
func (f *StorageFactory) createBaiduBOS() (Storage, error) {
	if f.config.BaiduEndpoint == "" || f.config.BaiduAccessKeyID == "" || f.config.BaiduAccessKeySecret == "" || f.config.BaiduBucketName == "" {
		return nil, fmt.Errorf("Baidu BOS configuration is incomplete")
	}
	return NewBaiduBOS(f.config), nil
}

// createAWSS3 creates an AWS S3 storage instance
func (f *StorageFactory) createAWSS3() (Storage, error) {
	if f.config.AWSRegion == "" || f.config.AWSAccessKeyID == "" || f.config.AWSSecretAccessKey == "" || f.config.AWSBucketName == "" {
		return nil, fmt.Errorf("AWS S3 configuration is incomplete")
	}
	return NewAWSS3(f.config), nil
}

// createAzureBlob creates an Azure Blob Storage instance
func (f *StorageFactory) createAzureBlob() (Storage, error) {
	if f.config.AzureAccountName == "" || f.config.AzureAccountKey == "" || f.config.AzureContainerName == "" {
		return nil, fmt.Errorf("Azure Blob Storage configuration is incomplete")
	}
	return NewAzureBlob(f.config), nil
}

// createQiniuKodo creates a Qiniu Kodo storage instance
func (f *StorageFactory) createQiniuKodo() (Storage, error) {
	if f.config.QiniuEndpoint == "" || f.config.QiniuAccessKey == "" || f.config.QiniuSecretKey == "" || f.config.QiniuBucketName == "" {
		return nil, fmt.Errorf("Qiniu Kodo configuration is incomplete")
	}
	return NewQiniuKodo(f.config), nil
}

// createGoogleStorage creates a Google Cloud Storage instance
func (f *StorageFactory) createGoogleStorage() (Storage, error) {
	if f.config.GoogleProjectID == "" || f.config.GoogleBucketName == "" {
		return nil, fmt.Errorf("Google Cloud Storage configuration is incomplete")
	}
	return NewGoogleStorage(f.config), nil
}

// createMinIO creates a MinIO storage instance
func (f *StorageFactory) createMinIO() (Storage, error) {
	if f.config.MinIOEndpoint == "" || f.config.MinIOAccessKey == "" || f.config.MinIOSecretKey == "" || f.config.MinIOBucketName == "" {
		return nil, fmt.Errorf("MinIO configuration is incomplete")
	}
	return NewMinIO(f.config), nil
}
